using System;
using System.Linq;
using System.Diagnostics.Contracts;

namespace FoundationLib.Mail 
{
	/// <summary>
	/// This class represents a variable found in a template by the template parser.
	/// </summary>
	public class TemplateVariable 
	{
		/// <summary>
		/// The variable name
		/// </summary>
		private string name;
		/// <summary>
		/// The variable display style
		/// </summary>
		private DisplayStyle style;
		/// <summary>
		/// The text serving as the variable placeholder in the template
		/// </summary>
		private string placeholderValue;


		/// <summary>
		/// Creates a new template variable.
		/// </summary>
		/// <exception cref="ArgumentNullException">A null-reference was passed.
		/// </exception>
		/// <param name="name">The variable name</param>
		/// <param name="placeholderValue">The variable placeholder value in the
		/// template</param>
		internal TemplateVariable(string name, string placeholderValue)
		{
			Contract.Requires(!String.IsNullOrWhiteSpace(name));
			Contract.Requires(!String.IsNullOrWhiteSpace(placeholderValue));

			this.name = name;
			this.placeholderValue = placeholderValue;
			this.style = DisplayStyle.Text;
		}

		/// <summary>
		/// Creates a new template variable.
		/// </summary>
		/// <exception cref="ArgumentNullException">A null-reference was passed.
		/// </exception>
		/// <param name="name">The variable name</param>
		/// <param name="placeholderValue">The variable placeholder value in the
		/// template</param>
		/// <param name="style">The variable display style</param>
		internal TemplateVariable(string name, string placeholderValue, DisplayStyle style)
			: this(name, placeholderValue)
		{
			this.style = style;
		}

		/// <summary>
		/// Gets the variable name.
		/// </summary>
		public string Name
		{
			get
			{
				return this.name;
			}
		}

		/// <summary>
		/// Gets the display style.
		/// </summary>
		public DisplayStyle Style
		{
			get
			{
				return this.style;
			}
		}

		/// <summary>
		/// Replaces all instances of the variable's placeholder in the given template with
		/// the value provided.
		/// </summary>
		/// <exception cref="ArgumentNullException">A null-reference was passed.
		/// </exception>
		/// <param name="templateContent">The template content</param>
		/// <param name="value">The new value</param>
		/// <returns>The result of the replacement</returns>
		public string Replace(string templateContent, string value)
		{
			Contract.Requires(!String.IsNullOrWhiteSpace(templateContent));
			return templateContent.Replace(this.placeholderValue, value);
		}

		/// <summary>
		/// Gets the text serving as the variable placeholder in the template.
		/// </summary>
		public string PlaceholderValue
		{
			get
			{
				return this.placeholderValue;
			}
		}

	}

}